import json

from django.shortcuts import HttpResponse
from django_filters.rest_framework import DjangoFilterBackend

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin
from rest_framework import serializers
from rest_framework.pagination import PageNumberPagination, CursorPagination
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import FilterSet, NumberFilter

from apps.minapp import models
from utils.tencent.oss import get_credential


class MenuSerializer(serializers.ModelSerializer):
    src = serializers.CharField()

    class Meta:
        model = models.Cover
        fields = "__all__"


class MenuCursorPagination(CursorPagination):
    ordering = 'id'
    page_size = 15


class MenuFilter(FilterSet):
    window = NumberFilter(field_name='detail__shop__id')

    class Meta:
        model = models.Cover
        fields = ['window']


class MenuView(ListModelMixin, RetrieveModelMixin, GenericViewSet):
    authentication_classes = []
    queryset = models.Cover.objects.all()
    serializer_class = MenuSerializer
    filter_backends = [OrderingFilter, DjangoFilterBackend]
    filterset_class = MenuFilter
    pagination_class = MenuCursorPagination
    ordering = ['-id', '-sales', 'price']


class OssCredentialView(APIView):

    def get(self, request, *args, **kwargs):
        return Response(get_credential())


class CoverSerializer(serializers.ModelSerializer):
    src = serializers.CharField()

    class Meta:
        model = models.Cover
        fields = "__all__"


class SrcSerialzer(serializers.ModelSerializer):
    src = serializers.CharField()

    class Meta:
        model = models.DetailSrc
        fields = "__all__"


class WindowSerializer(serializers.ModelSerializer):
    icon = serializers.CharField()

    class Meta:
        model = models.Window
        fields = "__all__"


class UserDetailSerializer(serializers.ModelSerializer):
    cover = CoverSerializer()
    detailsrc = SrcSerialzer(many=True)
    shop = WindowSerializer()

    class Meta:
        model = models.Detail
        fields = "__all__"
        depth = 1


# class DetailView(APIView):
#     authentication_classes = []
#
#     def get(self, request, *args, **kwargs):
#         id=kwargs['pk']
#         detail = models.Detail.objects.all().get(id)
#         ser = DetailSerializer(instance=detail, many=False)
#         # serializer_class = [serializers.ModelSerializer]
#         # queryset = models.Detail.objects.all()
#         print(ser.data)
#         ret = json.dumps(ser.data, ensure_ascii=False)
#         return Response(ser.data)
class StaffDetailSerializer(serializers.ModelSerializer):
    cover = CoverSerializer()
    detailsrc = SrcSerialzer(many=True)

    class Meta:
        model = models.Detail
        exclude = ['shop']
        depth = 1


class DetailView(RetrieveModelMixin, GenericViewSet):
    queryset = models.Detail.objects.all()
    serializer_class = UserDetailSerializer

    def get_serializer_class(self):
        user = self.request.user
        if isinstance(user, models.User):
            return UserDetailSerializer
        else:
            print("staff")
            return StaffDetailSerializer
